package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.po.Post;
import org.apache.ibatis.annotations.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface PostMapper extends BaseMapper<Post> {
    /**
     * 统计指定日期之后的活跃用户数
     */
    @Select("SELECT COUNT(DISTINCT user_id) FROM posts WHERE created_at >= #{startDate} ")
    Integer countActiveUsers(@Param("startDate") Date startDate);
    
    /**
     * 获取最近n天的每日活跃用户数
     */
    @Select("SELECT DATE_FORMAT(created_at, '%Y-%m-%d') as day, COUNT(DISTINCT user_id) as value " +
           "FROM posts " +
           "WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY) " +
           "GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d') " +
           "ORDER BY day DESC " +
           "LIMIT #{days}")
    List<Map<String, Object>> getDailyActiveUsers(@Param("days") Integer days);
    
    /**
     * 获取内容分类统计
     */
    @Select("SELECT " +
           "CASE content_type " +
           "    WHEN 1 THEN '图文' " +
           "    WHEN 2 THEN '视频' " +
           "    WHEN 3 THEN '链接' " +
           "    ELSE '其他' " +
           "END as name, " +
           "COUNT(*) as count, " +
           "ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM posts ), 1) as percent " +
           "FROM posts " +
           "GROUP BY content_type " +
           "ORDER BY count DESC")
    List<Map<String, Object>> getContentTypeStats();
    
    /**
     * 获取热门话题
     */
    @Select("SELECT t.topic_name as name, COUNT(DISTINCT p.post_id) as count " +
           "FROM topics t " +
           "INNER JOIN post_topics pt ON t.topic_id = pt.topic_id " +
           "INNER JOIN posts p ON p.post_id = pt.post_id " +
           "WHERE p.created_at >= #{startDate} " +
           "GROUP BY t.topic_id, t.topic_name " +
           "ORDER BY count DESC " +
           "LIMIT 7")
    List<Map<String, Object>> getHotTopics(@Param("startDate") Date startDate);
} 